    <!DOCTYPE html>
<html lang="zh-cn">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<meta name="author" content="nodejh">
		<meta name="description" content="My Blog">
		<meta name="generator" content="Hugo 0.24.1" />
		<title>C&#43;&#43; Commonly Used &middot; nodejh</title>
		<link rel="shortcut icon" href="http://nodejh.com/images/favicon.ico">
		<link rel="stylesheet" href="http://nodejh.com/css/style.css">
		<link rel="stylesheet" href="http://nodejh.com/css/highlight.css">
		

		
		<link rel="stylesheet" href="http://nodejh.com/css/font-awesome.min.css">
		

		
	</head>

    <body>
       <nav class="main-nav">
	
	
		<a href='http://nodejh.com/'> <span class="arrow">←</span>Home</a>
	
	<a href='http://nodejh.com/post'>Archive</a>
	<a href='http://nodejh.com/tags'>Tags</a>
	<a href='http://nodejh.com/projects'>Projects</a>
	<a href='http://nodejh.com/about'>About</a>

	

	
</nav>


        <section id="wrapper">
            <article class="post">
                <header>
                    <h1>
                        
                            C&#43;&#43; 常用操作
                        
                    </h1>
                    <h2 class="headline">
                    Jul 30, 2017 01:44
                    · 161 words
                    · 1 minute read
                      <span class="tags">
                      
                      
                          
                              <a href="http://nodejh.com/tags/c&#43;&#43;">C&#43;&#43;</a>
                          
                      
                      
                      </span>
                    </h2>
                </header>
                
                  
                    <div id="toc">
                      <nav id="TableOfContents">
<ul>
<li>
<ul>
<li><a href="#字符串转数字">字符串转数字</a></li>
<li><a href="#数字转字符串及长度">数字转字符串及长度</a></li>
<li><a href="#求字符串长度">求字符串长度</a></li>
<li><a href="#翻转数字">翻转数字</a></li>
</ul></li>
</ul>
</nav>
                    </div>
                  
                
                <section id="post-body">
                    

<p>最近在用 C++ 刷算法题。由于对 C++ 不熟悉，很多语法都是边查边用。为了方便查阅和记忆，所以在此记录下来。</p>

<h2 id="字符串转数字">字符串转数字</h2>

<pre><code class="language-cpp">string str=&quot;0100&quot;;
int i = stoi(str); // 100
</code></pre>

<h2 id="数字转字符串及长度">数字转字符串及长度</h2>

<pre><code class="language-cpp">#include &lt;string&gt;

std::string s = std::to_string(42);
int length = s.length();
int len=s.size();
</code></pre>

<ul>
<li><a href="https://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c">Easiest way to convert int to string in C++</a></li>
</ul>

<h2 id="求字符串长度">求字符串长度</h2>

<pre><code class="language-cpp">char s[3]=&quot;abc&quot;;
cout&lt;&lt;strlen(s)&lt;&lt;endl; // 3
``

## 翻转字符串

```cpp
string rev(string str)
{
  long length=str.lengt();
  int start=0, end=lengt-1;
  while(start&lt;end) {
    str[start]=start[end];
    start++;
    end--;
  }
  return str;
}
</code></pre>

<h2 id="翻转数字">翻转数字</h2>

<pre><code class="language-cpp">long long res=0;
while(x) {
  res = res*10 + x%10;
  x /= 10;
}
return (res &gt; INT_MAX || res &lt; INT_MIN) ? 0 : res;
</code></pre>

<ul>
<li><a href="https://leetcode.com/problems/reverse-integer/description/">7. Reverse Integer</a></li>
</ul>

                </section>
            </article>

            

            
                <div id="disqus_thread"></div>
<script type="text/javascript">
    var disqus_shortname = 'nodejh'; 

     
    (function() {
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>

            

            

            <footer id="footer">
    
        <div id="social">

	
	
    <a class="symbol" href="https://www.facebook.com/nodejh">
        <i class="fa fa-facebook-square"></i>
    </a>
    
    <a class="symbol" href="https://www.github.com/nodejh">
        <i class="fa fa-github-square"></i>
    </a>
    
    <a class="symbol" href="https://www.twitter.com/nodejh">
        <i class="fa fa-twitter-square"></i>
    </a>
    


</div>

    
    <p class="small">
    
       © Copyright 2017 <i class="fa fa-heart" aria-hidden="true"></i> nodejh
    
    </p>
    <p class="small">
        Powered by <a href="http://www.gohugo.io/">Hugo</a> Theme By <a href="https://github.com/nodejh/hugo-theme-cactus-plus">nodejh</a>
    </p>
</footer>

        </section>

        <script src="http://nodejh.com/js/jquery-2.2.4.min.js"></script>
<script src="http://nodejh.com/js/main.js"></script>
<script src="http://nodejh.com/js/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>




  
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-84989670-1', 'auto');
ga('send', 'pageview');
</script>




<script>
var baiduAnalytics = '39843ea392201290bd6f76173d2e1633';
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?" + baiduAnalytics;
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hm, s);
})();
</script>


    </body>
</html>
